{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# tensorboard运行数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "Iter0, Testing Accuracy:0.7449\n",
      "Iter1, Testing Accuracy:0.831\n",
      "Iter2, Testing Accuracy:0.8588\n",
      "Iter3, Testing Accuracy:0.8704\n",
      "Iter4, Testing Accuracy:0.8779\n",
      "Iter5, Testing Accuracy:0.8818\n",
      "Iter6, Testing Accuracy:0.8844\n",
      "Iter7, Testing Accuracy:0.8882\n",
      "Iter8, Testing Accuracy:0.8913\n",
      "Iter9, Testing Accuracy:0.894\n",
      "Iter10, Testing Accuracy:0.8956\n",
      "Iter11, Testing Accuracy:0.8968\n",
      "Iter12, Testing Accuracy:0.8986\n",
      "Iter13, Testing Accuracy:0.8992\n",
      "Iter14, Testing Accuracy:0.9009\n",
      "Iter15, Testing Accuracy:0.9013\n",
      "Iter16, Testing Accuracy:0.9031\n",
      "Iter17, Testing Accuracy:0.9032\n",
      "Iter18, Testing Accuracy:0.9042\n",
      "Iter19, Testing Accuracy:0.9051\n",
      "Iter20, Testing Accuracy:0.9054\n",
      "Iter21, Testing Accuracy:0.9067\n",
      "Iter22, Testing Accuracy:0.9068\n",
      "Iter23, Testing Accuracy:0.9072\n",
      "Iter24, Testing Accuracy:0.9077\n",
      "Iter25, Testing Accuracy:0.9081\n",
      "Iter26, Testing Accuracy:0.909\n",
      "Iter27, Testing Accuracy:0.9097\n",
      "Iter28, Testing Accuracy:0.9095\n",
      "Iter29, Testing Accuracy:0.91\n",
      "Iter30, Testing Accuracy:0.9102\n",
      "Iter31, Testing Accuracy:0.9107\n",
      "Iter32, Testing Accuracy:0.911\n",
      "Iter33, Testing Accuracy:0.9114\n",
      "Iter34, Testing Accuracy:0.9122\n",
      "Iter35, Testing Accuracy:0.9124\n",
      "Iter36, Testing Accuracy:0.9127\n",
      "Iter37, Testing Accuracy:0.9129\n",
      "Iter38, Testing Accuracy:0.9134\n",
      "Iter39, Testing Accuracy:0.9134\n",
      "Iter40, Testing Accuracy:0.9132\n",
      "Iter41, Testing Accuracy:0.9141\n",
      "Iter42, Testing Accuracy:0.9139\n",
      "Iter43, Testing Accuracy:0.915\n",
      "Iter44, Testing Accuracy:0.9152\n",
      "Iter45, Testing Accuracy:0.9147\n",
      "Iter46, Testing Accuracy:0.9156\n",
      "Iter47, Testing Accuracy:0.9154\n",
      "Iter48, Testing Accuracy:0.9156\n",
      "Iter49, Testing Accuracy:0.9163\n",
      "Iter50, Testing Accuracy:0.9158\n",
      "completed\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "# 载入数据集\n",
    "mnist=input_data.read_data_sets(\"MNIST_data\",one_hot=True)\n",
    "\n",
    "# 每个批次的大小\n",
    "batch_size=200 # 每次放入的数据量\n",
    "# 计算有多少个批次\n",
    "n_batch=mnist.train.num_examples // batch_size\n",
    "\n",
    "# 参数摘要：\n",
    "def variable_summaries(var):\n",
    "    with tf.name_scope('summaries'):\n",
    "        mean=tf.reduce_mean(var)\n",
    "        tf.summary.scalar('mean',mean)\n",
    "        with tf.name_scope('stddey'):\n",
    "            stddey=tf.sqrt(tf.reduce_mean(tf.square(var-mean)))\n",
    "        tf.summary.scalar('stddey',stddey)\n",
    "        tf.summary.scalar('max',tf.reduce_max(var))\n",
    "        tf.summary.scalar('min',tf.reduce_min(var))\n",
    "        tf.summary.histogram('histogram',var)\n",
    "\n",
    "# 命名空间\n",
    "with tf.name_scope('input'):\n",
    "   # 定义两个placeholder\n",
    "    x=tf.placeholder(tf.float32,[None,784],name='x-input')\n",
    "    y=tf.placeholder(tf.float32,[None,10],name='y-input') \n",
    "\n",
    "with tf.name_scope('layer'):\n",
    "    # 创建简单的神经网络\n",
    "    with tf.name_scope('weights'):\n",
    "        W=tf.Variable(tf.zeros([784,10]),name='W')\n",
    "        variable_summaries(W)\n",
    "    with tf.name_scope('biases'):\n",
    "        b=tf.Variable(tf.zeros([10]),name='b')\n",
    "        variable_summaries(b)\n",
    "    with tf.name_scope('wx_plus_b'):\n",
    "        wx_plus_b=tf.matmul(x,W)+b\n",
    "    with tf.name_scope('softmax'):\n",
    "        prediction=tf.nn.softmax(wx_plus_b)    \n",
    "    \n",
    "\n",
    "\n",
    "# 二次代价函数\n",
    "with tf.name_scope('loss'):\n",
    "    loss=tf.reduce_mean(tf.square(y-prediction))\n",
    "    tf.summary.scalar('loss',loss)\n",
    "# 使用梯度下降法\n",
    "with tf.name_scope('train'):\n",
    "    train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "\n",
    "# 记过存放在布尔型列表中\n",
    "with tf.name_scope('accuracy'):\n",
    "    with tf.name_scope('correct_prediction'):\n",
    "        correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) # 最大值所在位置\n",
    "# 求准确率\n",
    "    with tf.name_scope('accuracy'):\n",
    "        accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "        tf.summary.scalar('accuracy',accuracy)\n",
    "\n",
    "# 合并所有的summary\n",
    "merged=tf.summary.merge_all()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    writer=tf.summary.FileWriter('logs/',sess.graph)\n",
    "    for epoch in range(51):\n",
    "        for batch in range(n_batch):\n",
    "            batch_xs,batch_ys=mnist.train.next_batch(batch_size)\n",
    "            summary,_=sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys})\n",
    "            \n",
    "        writer.add_summary(summary,epoch)\n",
    "        acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})\n",
    "        print(\"Iter\"+str(epoch)+\", Testing Accuracy:\"+str(acc))\n",
    "\n",
    "print('completed')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "Iter0, Testing Accuracy:0.7405\n",
      "Iter1, Testing Accuracy:0.8302\n",
      "Iter2, Testing Accuracy:0.8587\n",
      "Iter3, Testing Accuracy:0.8711\n",
      "Iter4, Testing Accuracy:0.8769\n",
      "Iter5, Testing Accuracy:0.8818\n",
      "Iter6, Testing Accuracy:0.8851\n",
      "Iter7, Testing Accuracy:0.8878\n",
      "Iter8, Testing Accuracy:0.8911\n",
      "Iter9, Testing Accuracy:0.8948\n",
      "Iter10, Testing Accuracy:0.8955\n",
      "Iter11, Testing Accuracy:0.898\n",
      "Iter12, Testing Accuracy:0.8987\n",
      "Iter13, Testing Accuracy:0.8999\n",
      "Iter14, Testing Accuracy:0.9005\n",
      "Iter15, Testing Accuracy:0.9022\n",
      "Iter16, Testing Accuracy:0.9031\n",
      "Iter17, Testing Accuracy:0.9042\n",
      "Iter18, Testing Accuracy:0.9048\n",
      "Iter19, Testing Accuracy:0.9053\n",
      "Iter20, Testing Accuracy:0.9057\n",
      "Iter21, Testing Accuracy:0.9062\n",
      "Iter22, Testing Accuracy:0.9065\n",
      "Iter23, Testing Accuracy:0.9071\n",
      "Iter24, Testing Accuracy:0.908\n",
      "Iter25, Testing Accuracy:0.9084\n",
      "Iter26, Testing Accuracy:0.9086\n",
      "Iter27, Testing Accuracy:0.9094\n",
      "Iter28, Testing Accuracy:0.9095\n",
      "Iter29, Testing Accuracy:0.9099\n",
      "Iter30, Testing Accuracy:0.9103\n",
      "Iter31, Testing Accuracy:0.9108\n",
      "Iter32, Testing Accuracy:0.9115\n",
      "Iter33, Testing Accuracy:0.9115\n",
      "Iter34, Testing Accuracy:0.9116\n",
      "Iter35, Testing Accuracy:0.9126\n",
      "Iter36, Testing Accuracy:0.9131\n",
      "Iter37, Testing Accuracy:0.9128\n",
      "Iter38, Testing Accuracy:0.9133\n",
      "Iter39, Testing Accuracy:0.9139\n",
      "Iter40, Testing Accuracy:0.9134\n",
      "Iter41, Testing Accuracy:0.9135\n",
      "Iter42, Testing Accuracy:0.9141\n",
      "Iter43, Testing Accuracy:0.9152\n",
      "Iter44, Testing Accuracy:0.9151\n",
      "Iter45, Testing Accuracy:0.9147\n",
      "Iter46, Testing Accuracy:0.9155\n",
      "Iter47, Testing Accuracy:0.9153\n",
      "Iter48, Testing Accuracy:0.9156\n",
      "Iter49, Testing Accuracy:0.9159\n",
      "Iter50, Testing Accuracy:0.9159\n",
      "completed\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "# 载入数据集\n",
    "mnist=input_data.read_data_sets(\"MNIST_data\",one_hot=True)\n",
    "\n",
    "# 每个批次的大小\n",
    "batch_size=200 # 每次放入的数据量\n",
    "# 计算有多少个批次\n",
    "n_batch=mnist.train.num_examples // batch_size\n",
    "\n",
    "# 参数摘要：\n",
    "def variable_summaries(var):\n",
    "    with tf.name_scope('summaries'):\n",
    "        mean=tf.reduce_mean(var)\n",
    "        tf.summary.scalar('mean',mean)\n",
    "        with tf.name_scope('stddey'):\n",
    "            stddey=tf.sqrt(tf.reduce_mean(tf.square(var-mean)))\n",
    "        tf.summary.scalar('stddey',stddey)\n",
    "        tf.summary.scalar('max',tf.reduce_max(var))\n",
    "        tf.summary.scalar('min',tf.reduce_min(var))\n",
    "        tf.summary.histogram('histogram',var)\n",
    "\n",
    "# 命名空间\n",
    "with tf.name_scope('input'):\n",
    "   # 定义两个placeholder\n",
    "    x=tf.placeholder(tf.float32,[None,784],name='x-input')\n",
    "    y=tf.placeholder(tf.float32,[None,10],name='y-input') \n",
    "\n",
    "with tf.name_scope('layer'):\n",
    "    # 创建简单的神经网络\n",
    "    with tf.name_scope('weights'):\n",
    "        W=tf.Variable(tf.zeros([784,10]),name='W')\n",
    "        variable_summaries(W)\n",
    "    with tf.name_scope('biases'):\n",
    "        b=tf.Variable(tf.zeros([10]),name='b')\n",
    "        variable_summaries(b)\n",
    "    with tf.name_scope('wx_plus_b'):\n",
    "        wx_plus_b=tf.matmul(x,W)+b\n",
    "    with tf.name_scope('softmax'):\n",
    "        prediction=tf.nn.softmax(wx_plus_b)    \n",
    "    \n",
    "\n",
    "\n",
    "# 二次代价函数\n",
    "with tf.name_scope('loss'):\n",
    "    loss=tf.reduce_mean(tf.square(y-prediction))\n",
    "    tf.summary.scalar('loss',loss)\n",
    "# 使用梯度下降法\n",
    "with tf.name_scope('train'):\n",
    "    train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "\n",
    "# 记过存放在布尔型列表中\n",
    "with tf.name_scope('accuracy'):\n",
    "    with tf.name_scope('correct_prediction'):\n",
    "        correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) # 最大值所在位置\n",
    "# 求准确率\n",
    "    with tf.name_scope('accuracy'):\n",
    "        accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "        tf.summary.scalar('accuracy',accuracy)\n",
    "\n",
    "# 合并所有的summary\n",
    "merged=tf.summary.merge_all()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    writer=tf.summary.FileWriter('logs/',sess.graph)\n",
    "    for epoch in range(51):\n",
    "        for batch in range(n_batch):\n",
    "            batch_xs,batch_ys=mnist.train.next_batch(batch_size)\n",
    "            summary,_=sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys})\n",
    "            \n",
    "        writer.add_summary(summary,epoch)\n",
    "        acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})\n",
    "        print(\"Iter\"+str(epoch)+\", Testing Accuracy:\"+str(acc))\n",
    "\n",
    "print('completed')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
